package com.tuanzi.loan.web.bean.family;

import com.tuanzi.loan.business.service.family.IcpWebSensitiveKeywordService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFListingBean;

import com.tuanzi.loan.vo.request.family.IcpWebSensitiveKeywordSearchRequest;
import com.tuanzi.loan.vo.response.family.IcpWebSensitiveKeywordSearchResponse;
import com.tuanzi.loan.web.lazy.family.IcpWebSensitiveKeywordDataModel;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

/**
 * ICP备案网站敏感字管理
 * @author JinRuiyang 2017/12/13 17:56:34
 */
@Debug
@Data
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class IcpWebSensitiveKeywordBean extends JSFListingBean<IcpWebSensitiveKeywordSearchRequest, IcpWebSensitiveKeywordSearchResponse> {

    private static final int Published = 1;
    private static final int UnPublish = 0;

    @Autowired
    private IcpWebSensitiveKeywordService icpWebSensitiveKeywordService;

    @PostConstruct
    private void init() {
        initLazyDataModel();
    }

    public void onReset() {
        initLazyDataModel();
    }

    public void onPublish(Long id){
        try {
            icpWebSensitiveKeywordService.updatePublishStatus(id, Published);
        } catch (RuntimeException e){
            addErrorMessage(e.getMessage());
        }
    }

    public void onBatchPublish(){
        for (IcpWebSensitiveKeywordSearchResponse response : selectedRecords){
            onPublish(response.getPk());
        }
    }

    public void onUnPublish(Long id){
        try {
            icpWebSensitiveKeywordService.updatePublishStatus(id, UnPublish);
        } catch (RuntimeException e){
            addErrorMessage(e.getMessage());
        }
    }

    public void onBatchUnPublish(){
        for (IcpWebSensitiveKeywordSearchResponse response : selectedRecords){
            onUnPublish(response.getPk());
        }
    }

    public void onDelete(Long id){
        try {
            icpWebSensitiveKeywordService.delete(id);
        } catch (RuntimeException e){
            addErrorMessage(e.getMessage());
        }
    }

    public void onBatchDelete(){
        for (IcpWebSensitiveKeywordSearchResponse response : selectedRecords){
            onDelete(response.getPk());
        }
    }

    public void onCreate(){
        try {
            icpWebSensitiveKeywordService.insert($.getKeyword());
            $.setKeyword("");
        } catch (RuntimeException e){
            addErrorMessage(e.getMessage());
        }
    }

    public void onRefresh(){
        lazyDataModel = new IcpWebSensitiveKeywordDataModel($);
        addSuccessfullyMessage();
    }

    private void initLazyDataModel() {
        $ = new IcpWebSensitiveKeywordSearchRequest();
        lazyDataModel = new IcpWebSensitiveKeywordDataModel($);
    }
}